/**
 * 
 */
package org.psneog.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.sql.*;

/**
 * @author nits
 *
 */
public class JDBCDerbyDemo {

	private String drivername = new String("org.apache.derby.jdbc.EmbeddedDriver");
	private Connection conn;
	private String dburl = new String("jdbc:derby:/home/nits/MyDB;create=true");
	
	static void log(String msg){
		System.out.println(msg);
	}
	
	private void connectDB() throws Exception {
		log("Registering driver...");
		try {
			Class.forName(drivername);
		} catch (ClassNotFoundException cnfe) {
			log("FAIL: Class not found\nfor details see stacktrace below:");
			cnfe.printStackTrace();
			throw cnfe;
		}
		
		try {
			log("Connecting to database:");
			conn = DriverManager.getConnection(dburl);
			log("Connected to "+dburl);
		}
		catch(SQLException sqe1) {
			log("Error");
			sqe1.printStackTrace();
			System.exit(-1);
		}
	}
	
	void close() {
		log("Closing connection "+conn.toString());
		try {
			conn.close();
		} catch(SQLException sqe2) {
				log("Close Error");
				sqe2.printStackTrace();
				System.exit(-1);
		}
	}
	

	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		log("Program starts");
		// TODO Auto-generated method stub
		JDBCDerbyDemo demo = new JDBCDerbyDemo();
	
		try {
			demo.connectDB();
			demo.close();
		} catch (Exception e) {
			e.printStackTrace();
		
		}
		log("bye");
	}

}
